package cn.sumpu.app.coolsk.biz.impl;



//import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.coolsk.biz.CompanyBiz;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.CompanyDao;
import cn.sumpu.app.coolsk.domain.Company;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class CompanyBizImpl implements CompanyBiz{

	private CompanyDao companyDao;
	
	@Autowired
	private IdentifierBizImpl identifierBiz;
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	@Override
	public PageModel getCompanyList(Integer page, Integer pagesize,String searchName) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>(); 
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		if(!Common.empty(searchName)){
			map.put("searchName", "'%"+searchName+"%'");
		}
		
		int count = companyDao.getCompanyCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(companyDao.getCompany(map));
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}

	@Override
	public Company getCompanyById(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Company> tlist = companyDao.getCompany(map);
		if(tlist != null && tlist.size() > 0){
			return tlist.get(0);
		}
		return null;
	}

	@Override
	public Company saveCompany(Company company) throws AppBusinessException {
		if(company == null )return null;
		String id = company.getId();
		if(Common.empty(id)){
			company.setId(identifierBiz.generateId(Table.COMPANY));
			companyDao.insertCompany(company);
		}else{
			companyDao.updateCompany(company);
		}
		
		return company;
	}

	@Override
	public boolean delCompany(String id) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		companyDao.delCompany(map);
		return true;
	}
	/**
	 * author BruceZhang
	 * @param name	公司名称
	 * @return	联想公司名称
	 * @throws AppBusinessException
	 */
	@Override
	public List<Company> getCompanyByName(String name)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", name);
		return companyDao.getCompanyByName(map);
	}
	
}
